from ui.components.base.widget import TgBaseWidget
from .views.options_volatility_view import OptionsVolatilityView
from .controllers.options_volatility_controller import OptionsVolatilityController
from loguru import logger


class OptionsVolatilityTab(TgBaseWidget):
    """期权波动率 Tab - MVC架构重构版本"""

    def __init__(self):
        super().__init__()
        self.view = OptionsVolatilityView()
        self.controller = OptionsVolatilityController()
        
        self._setup_mvc()
        self._initialize()

    def _setup_mvc(self):
        from ui.components.layouts.vbox_layout import TgVBoxLayout
        TgVBoxLayout(self).addWidget(self.view)
        self._connect_signals()

    def _connect_signals(self):
        # Skew - 已注释掉波动率偏斜曲线相关功能，但保留查询按钮用于波动率锥
        self.view.query_button.clicked.connect(self._on_query_cone_clicked)
        # self.controller.data_loaded.connect(self.view.update_volatility_skew)
        # self.controller.loading_state_changed.connect(self.view.set_query_loading)
        
        # Cone (shares same query as skew)
        self.controller.cone_data_with_previous_loaded.connect(self.view.update_cone_chart_with_previous)

        # Historical IV
        self.view.query_historical_iv_button.clicked.connect(self._on_query_historical_iv_clicked)
        self.controller.historical_iv_data_loaded.connect(self.view.update_historical_iv_chart)
        self.controller.historical_iv_loading_state_changed.connect(self.view.set_historical_iv_query_loading)

        # IV History
        self.view.query_iv_history_button.clicked.connect(self._on_query_iv_history_clicked)
        self.controller.iv_history_data_loaded.connect(self.view.update_iv_history_chart)
        self.controller.iv_history_loading_state_changed.connect(self.view.set_iv_history_query_loading)

        # IV Surface 3D
        self.view.query_iv_surface_button.clicked.connect(self._on_query_iv_surface_clicked)
        self.controller.iv_surface_data_loaded.connect(self.view.update_iv_surface_3d_chart)
        self.controller.iv_surface_loading_state_changed.connect(self.view.set_iv_surface_query_loading)

    def _initialize(self):
        # 设置标的符号
        symbols = self.controller.get_underlying_symbols()
        self.view.setup_underlying_symbols(symbols)
        
        # 设置默认日期
        default_date = self.controller.get_default_date()
        self.view.set_default_date(default_date)

    def _on_query_cone_clicked(self):
        """只查询波动率锥曲线，不查询偏斜曲线"""
        underlying_symbol, target_date = self.view.get_query_params()
        # 只查询波动率锥，不查询偏斜
        self.controller.query_options_cone(underlying_symbol, target_date)

    def _on_query_historical_iv_clicked(self):
        underlying_symbol, start_date, end_date, option_class = self.view.get_historical_iv_query_params()
        self.controller.query_historical_iv(underlying_symbol, start_date, end_date, option_class)

    def _on_query_iv_history_clicked(self):
        underlying_symbol, start_date, end_date = self.view.get_iv_history_query_params()
        self.controller.query_iv_history(underlying_symbol, start_date, end_date)

    def _on_query_iv_surface_clicked(self):
        underlying_symbol, date, option_class = self.view.get_iv_surface_query_params()
        self.controller.query_iv_surface(underlying_symbol, date, option_class)

    def cleanup(self):
        """清理资源"""
        self.view.clear_charts()